package com.hmkj.core.mapper.amount;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.amount.CashModel;
import com.hmkj.core.po.amount.Cash;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 操作权限相关Dao接口<br>
 *
 * @author donfy
 * @date 2017-08-15 08:16:12
 */
public interface CashMapper extends GenericMapper<Cash, Integer> {

    @Select("SELECT t.*,u.user_name AS 'u.userName', u.mobile AS 'u.mobile', u.real_name AS 'u.realName' FROM fm_cash t LEFT JOIN biz_user u ON u.id = t.user_id WHERE 1=1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize} ")
    List<CashModel> findListBySqlJoin(Map<String, Object> params);

    @Select("SELECT count(1)  FROM fm_cash t LEFT JOIN biz_user u ON u.id = t.user_id WHERE 1=1 ${whereSql}")
    long countBySqlJoin(Map<String, Object> params);

    /**
     * 根据订单号查询提现订单
     *
     * @param orderNo 订单号
     * @return
     */
    @Select("SELECT * FROM fm_cash WHERE 1=1 and order_no = #{orderNo} and del_flag = 1")
    Cash selectByOrderNo(@Param(value = "orderNo") String orderNo);

    /**
     * 根据订单号查询三方订单号
     *
     * @param loanNo 订单号
     * @return
     */
    @Select("SELECT * FROM fm_cash WHERE 1=1 and out_order_no = #{loanNo} and del_flag = 1")
    Cash selectByLoanNo(@Param(value = "loanNo") String loanNo);

    /**
     * 前台提现记录列表
     *
     * @param param
     * @return
     */
    @Select(" <script> SELECT t.* " +
            "FROM fm_cash t WHERE t.user_id = #{userId}" +
            "<if test=\"type != null and type != ''\"> AND t.`type` = #{type} </if>" +
            "ORDER BY t.create_time DESC </script>")
    List<Cash> findListForCashList(Map<String, Object> param);

    /**
     * 前台提现记录列表
     *
     * @param param
     * @return
     */
    @Select(" <script> SELECT count(*) " +
            "FROM fm_cash t WHERE t.user_id = #{userId}" +
            "<if test=\"type != null and type != ''\"> AND t.`type` = #{type} </if>" +
            "ORDER BY t.create_time DESC </script>")
    Integer findCountForCashList(Map<String, Object> param);

    /**
     * 获取提现总金额
     *
     * @param userId
     * @return
     */
    @Select("SELECT IFNULL(SUM(t.amount),0.00) AS 'cashMoney' FROM fm_cash t WHERE t.user_id = #{userId} AND t.status <> 5 AND t.del_flag = 1")
    BigDecimal findCashAllMoney(@Param(value = "userId") Integer userId);

    /**
     * 查询当日期限记录（不包含提现失败）
     *
     * @param userId 用户ID
     * @return
     */
    @Select("SELECT t.* FROM fm_cash t WHERE t.user_id = #{userId} AND t.status <> 5 AND t.create_time like CONCAT(date_format(now(),'%Y-%m-%d'),'%') AND t.del_flag = 1")
    List<Cash> selectDailyCash(@Param(value = "userId") Integer userId);

    /**
     * 查询过期的未处理提现信息
     *
     * @param expiredTime 过期时间
     * @return
     */
    @Select("SELECT t.* FROM fm_cash t WHERE t.del_flag = 1 AND t.status = 1 AND t.create_time <= #{expiredTime} ORDER BY t.create_time ASC")
    List<Cash> selectExpired(@Param(value = "expiredTime") Date expiredTime);

    /**
     * 更新提现状态
     *
     * @param id        提现ID
     * @param newStatus 新状态
     * @param orgStatus 原状态
     * @return
     */
    @Update("UPDATE fm_cash SET status = #{newStatus} WHERE id = #{id} AND status = #{orgStatus} AND del_flag = 1")
    int updateCashStatus(@Param(value = "id") Integer id, @Param(value = "newStatus") Integer newStatus, @Param(value = "orgStatus") Integer orgStatus);

    /**
     * 根据状态统计提现金额
     * @param param
     * @return
     */
    @Select("<script> SELECT SUM(t.amount) FROM fm_cash t LEFT JOIN biz_user user ON user.id = t.user_id WHERE t.`status` = #{status} AND t.del_flag = 1 " +
            "<if test=\"startTime != null and startTime != ''\"> AND t.`create_time` >= #{startTime} </if>" +
            "<if test=\"endTime != null and endTime != ''\"> AND t.`create_time` &lt;= #{endTime} </if>" +
            "<if test=\"agentId != null and agentId != ''\"> AND user.`agent_id` = #{agentId} </if> </script>")
    BigDecimal selectSumAmountByStatus(Map<String, Object> param);

    /**
     * 根据状态统计提现笔数
     * @param param
     * @return
     */
    @Select("<script> SELECT COUNT(1) FROM fm_cash t LEFT JOIN biz_user user ON user.id = t.user_id WHERE t.`status` = #{status} AND t.del_flag = 1" +
            "<if test=\"startTime != null and startTime != ''\"> AND t.`create_time` >= #{startTime} </if>" +
            "<if test=\"endTime != null and endTime != ''\"> AND t.`create_time` &lt;= #{endTime} </if>" +
            "<if test=\"agentId != null and agentId != ''\"> AND user.`agent_id` = #{agentId} </if> </script>")
    long countByStatus(Map<String, Object> param);
}
